package com.iot.platform.mvc.thing.controller;

import java.util.List;

import javax.validation.Valid;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.iot.common.core.exception.ThingModelAbilityException;
import com.iot.common.core.util.R;
import com.iot.common.core.util.UuidUtil;
import com.iot.common.security.annotation.SecurityAuthorization;
import com.iot.platform.api.domain.ThingAbility;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/thing/ability")
@Api(value = "ThingAbilityController", tags = "功能")
public class ThingAbilityController {

	@SecurityAuthorization
	@GetMapping(value = { "/list" }, produces = "application/json; charset=utf-8")
	public R<Page<ThingAbility>> list(
			@RequestParam(name = "productId", required = true, defaultValue = "") String productId,
			@RequestParam(name = "keywords", required = false, defaultValue = "") String keywords,
			@RequestParam(name = "moduleId", required = false, defaultValue = "") String moduleId,
			@RequestParam(name = "isPush", required = false, defaultValue = "0") String isPush,
			@RequestParam(name = "cur", required = false, defaultValue = "1") int cur,
			@RequestParam(name = "limit", required = false, defaultValue = "10") int limit) throws Exception {
		String sqlExceptSelect = " FROM " + ThingAbility.TABLE_NAME + " O  WHERE 1=1 ";
		if (!StrKit.isBlank(keywords)) {
			sqlExceptSelect += " AND ( O.NAME LIKE '%" + keywords + "%' OR   O.IDENTIFIER LIKE '%" + keywords + "%' ) ";
		}
		if (!StrKit.isBlank(moduleId)) {
			sqlExceptSelect += " AND O.MODULE_ID = '" + moduleId + "'  ";
		}
		if (!StrKit.isBlank(productId)) {
			sqlExceptSelect += " AND O.PRODUCT_ID = '" + productId + "'  ";
		}
		if (!StrKit.isBlank(isPush)) {
			// 编辑状态下移出的字段不显示
			if (StrKit.equals("0", isPush)) {
				sqlExceptSelect += " AND O.IS_REMOVE = '0'  ";
			}
			if(!StrKit.equals("0", isPush))
				sqlExceptSelect += " AND O.IS_PUSH = '" + isPush + "'  ";
		}
		return R.ok(ThingAbility.dao.paginate(cur, limit, "SELECT * ", sqlExceptSelect));
	}

	@SecurityAuthorization
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<ThingAbility> save(@Valid ThingAbility ability) {
		if (ability == null) {
			throw new ThingModelAbilityException("参数必填！");
		}
		if (StrKit.isBlank(ability.getId())) {
			ability.setId(UuidUtil.getUUID());
			if (ability.save()) {
				return R.ok();
			}
		} else {
			if (ability.update()) {
				return R.ok();
			}
		}
		return R.failed();
	}

	@SecurityAuthorization
	@RequestMapping(value = "/getById", method = RequestMethod.GET)
	public R<ThingAbility> getById(
			@RequestParam(name = "abilityId", required = false, defaultValue = "") String abilityId) {

		ThingAbility ability = ThingAbility.dao.findById(abilityId);
		if (ability == null) {
			throw new ThingModelAbilityException("功能不存在！");
		}
		return R.ok(ability);
	}

	@SecurityAuthorization
	@PostMapping(value = { "/deleteById" }, produces = "application/json; charset=utf-8")
	public R<Boolean> deleteById(@RequestParam(name = "id", required = true) String id) throws Exception {
		ThingAbility ability=ThingAbility.dao.findById(id);
		if(ability==null) {
			return R.failed("数据不存在！",false);
		}
		ability.setIsRemove("1");
		if (ability.update()) {
			return R.ok("移出成功！",true);
		} else {
			return R.failed("移出失败！",false);
		}
	}

	@SecurityAuthorization
	@PostMapping(value = { "/push" }, produces = "application/json; charset=utf-8")
	public R<Boolean> push(@RequestParam(name = "id", required = true) String id) throws Exception {
		ThingAbility ability = ThingAbility.dao.findById(id);
		if (ability == null) {
			return R.failed("数据不存在！", false);
		}
		if (ThingAbility.dao.deleteById(id)) {
			return R.ok(true);
		} else {
			return R.failed(false);
		}
	}

	@SecurityAuthorization
	@PostMapping(value = { "/update" }, produces = "application/json; charset=utf-8")
	public R<Boolean> update(@RequestParam(name = "id", required = true) String id,
			@RequestParam(name = "dataSpecs", required = false) String dataSpecs) throws Exception {
		ThingAbility ability = ThingAbility.dao.findById(id);
		if (ability == null) {
			return R.failed("数据不存在！", false);
		}
		ability.setDataSpecs(dataSpecs);
		if (ability.update()) {
			return R.ok("更新成功！", true);
		} else {
			return R.failed(false);
		}
	}

	@SecurityAuthorization
	@PostMapping(value = { "/releaseOnline" }, produces = "application/json; charset=utf-8")
	public R<Boolean> releaseOnline(@RequestParam(name = "productId", required = true) String productId)
			throws Exception {
		List<ThingAbility> removeThingAbility=ThingAbility.dao.find("select id from "+ThingAbility.TABLE_NAME+" where IS_REMOVE='1' AND  PRODUCT_ID=? ",productId);
		for(ThingAbility ability:removeThingAbility) {
			if(!ability.delete()) {
				throw new Exception("发布失败！");
			}
		}
		List<ThingAbility> pushThingAbility=ThingAbility.dao.find("select * from "+ThingAbility.TABLE_NAME+" WHERE IS_REMOVE='0' AND IS_PUSH='0' AND  PRODUCT_ID=? ",productId);
		for(ThingAbility ability:pushThingAbility) {
			ability.setIsPush("1");
			if(!ability.update()) {
				throw new Exception("发布失败！");
			}
		}
		return R.ok("发布成功！", true);
	}
	
	@SecurityAuthorization
	@GetMapping(value = { "/getSelectAbility" }, produces = "application/json; charset=utf-8")
	public R<List<ThingAbility>> getSelectForEvent(@RequestParam(name = "productId", required = true) String moduleId,
			@RequestParam(name = "abilityType", required = false,defaultValue = "0") String abilityType) throws Exception {
		List<ThingAbility> eventAbilities=ThingAbility.dao.find("select id,name from "+ThingAbility.TABLE_NAME+" where MODULE_ID=? AND ABILITY_TYPE='"+abilityType+"' ",moduleId);
		return R.ok(eventAbilities);
	}

}
